#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/8 16:57
# @Author  : jeff
# @FileName: app_config
from flask import Flask
import atexit
from flask_cors import CORS
from src.config.cache_config import cache
from src.config.scheduler_config import Scheduler
from src.config.siwa_config import siwa
from src.filter.exception_aspect import business_exception, application_exception
from src.filter.middleware_aspect import middleware
from src.filter.sqlalchemy_aspect import sql_loguru
from src.config.blueprints_config import register_blueprints
from src.config.db_config import config
from src.model.base_model import db
from src.filter.shutdown_application import shutdown_application
import flask_excel as excel


def create_app():
    """
    Create app and register related components
     CORS(app, resources=r'/*') # Register cross-domain
     SiwaDoc(app) #Register interface document
     db.init_app(config(app)) # Register database
     business_exception(app) # Register business exception
     application_exception(app) #Register system exception
     atexit.register(shutdown_application) # Shut down the thread pool
     middleware(app) #Register middleware
     sql_loguru(app) # Register sql middleware and print sql logs
     register_blueprints(app) # Route registration
     texit.register(shutdown_application) # Close the thread pool
     scheduler # Initialize the scheduled task scheduler, otherwise the first call will be slow
     cache.init_app(app) # Register cache
     excel.init_excel(app) #Initialize excel
    :return: app
    """
    app = Flask(__name__)
    CORS(app, resources=r'/*')
    siwa.init_app(app)
    db.init_app(config(app))
    business_exception(app)
    application_exception(app)
    middleware(app)
    sql_loguru(app)
    register_blueprints(app)
    atexit.register(shutdown_application)
    Scheduler().init_apscheduler(app)
    cache.init_app(app)
    excel.init_excel(app)
    return app
